/**
 * Created with IntelliJ IDEA.
 * User: aermakov
 * Date: 23.05.13
 * Time: 15:19
 * To change this template use File | Settings | File Templates.
 */

Ext.ns('GrailsCompany.data');

GrailsCompany.data.DataHelper = Ext.extend(Ext.util.Observable, {
    constructor: function () {
        this.loadedCount = 0;
        this.addEvents({
            'loadAll': true,
            'update': true
        });

        this.Stores.All = [
            this.Stores.Employee, this.Stores.Project, this.Stores.Department, this.Stores.Organization
        ];

        Ext.each(this.Stores.All, function (it) {
            it.on({
                scope: this,
                load: this.handleStoreLoad,
                create: this.handleStoreCreate,
                remove: this.handleStoreDelete,
                update: this.handleStoreUpdate
            });
        }, this);

        GrailsCompany.data.DataHelper.superclass.constructor.call(this);
    },

    handleStoreUpdate: function (store, records) {
        Ext.each(records, function (r) {
            this.fireEvent('update', r, store.storeId, 'update');
        }, this);
    },

    handleStoreCreate: function (store, records) {
        Ext.each(records, function (r) {
            this.fireEvent('update', r, store.storeId, 'create');
        }, this);
    },

    handleStoreDelete: function (store, record) {
        this.fireEvent('update', record, store.storeId, 'delete');
    },

    handleStoreLoad: function () {
        this.loadedCount++;
        if (this.loadedCount === this.Stores.All.length) {
            this.fireEvent('loadAll');
        }
    },

    delete: function (storeId, recordId) {
        var store = Ext.StoreMgr.get(storeId);
        var record = store.getById(recordId);
        if (record) {
            store.remove(record);
        }
    },

    create: function (storeId, values) {
        var store = Ext.StoreMgr.get(storeId);
        var r = new store.recordType();
        r.beginEdit();
        for (var key in values) {
            r.set(key, values[key]);
        }
        r.endEdit();
        store.add(r);
    },

    update: function (storeId, recordId, values) {
        var store = Ext.StoreMgr.get(storeId);
        var r = store.getById(recordId);
        r.beginEdit();
        for (var key in values) {
            r.set(key, values[key]);
        }
        r.endEdit();
    },

    Stores: {
        Employee: new GrailsCompany.data.WritableJsonStore({
            storeId: 'employee',
            parentStoreId: 'project',
            label: 'Employee',
            proxy: new Ext.data.HttpProxy({
                url: 'employee/list',
                api: {
                    create: 'employee/create',
                    read: 'employee/view',
                    update: 'employee/update',
                    destroy: 'employee/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', {name: 'parentId', type: 'int'},
                'firstName', 'lastName', 'emailAddress', 'accountName',
                'vatId', 'cardNumber']

        }),
        Project: new GrailsCompany.data.WritableJsonStore({
            storeId: 'project',
            parentStoreId: 'department',
            childStoreId: 'employee',
            label: 'Project',
            proxy: new Ext.data.HttpProxy({
                url: 'project/list',
                api: {
                    create: 'project/create',
                    read: 'project/view',
                    update: 'project/update',
                    destroy: 'project/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', 'title', {name: 'parentId', type: 'int'}, 'description']
        }),
        Department: new GrailsCompany.data.WritableJsonStore({
            storeId: 'department',
            parentStoreId: 'organization',
            childStoreId: 'project',
            label: 'Department',
            proxy: new Ext.data.HttpProxy({
                url: 'department/list',
                api: {
                    create: 'department/create',
                    read: 'department/view',
                    update: 'department/update',
                    destroy: 'department/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', 'title', {name: 'parentId', type: 'int'}, 'description']
        }),
        Organization: new GrailsCompany.data.WritableJsonStore({
            storeId: 'organization',
            childStoreId: 'department',
            label: 'Organization',
            proxy: new Ext.data.HttpProxy({
                url: 'organization/list',
                api: {
                    create: 'organization/create',
                    read: {url: 'organization/view', method: 'GET'},
                    update: 'organization/update',
                    destroy: 'organization/delete'
                }
            }),
            idProperty: 'id',
            fields: ['id', 'title', 'description']
        })
    }
});

GrailsCompany.data.DH = function () {
    return new GrailsCompany.data.DataHelper;
}();